package cn.iocoder.yudao.module.hs.service.shiporderdetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.hs.controller.admin.shiporderdetail.vo.ShipOrderDetailPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.shiporderdetail.vo.ShipOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.shiporderdetail.ShipOrderDetailDO;
import cn.iocoder.yudao.module.hs.dal.mysql.shiporderdetail.ShipOrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.SHIP_ORDER_DETAIL_NOT_EXISTS;

/**
 * 出库单明细 Service 实现类
 *
 * @author 航盛
 */
@Service
@Validated
public class ShipOrderDetailServiceImpl implements ShipOrderDetailService {

    @Resource
    private ShipOrderDetailMapper shipOrderDetailMapper;

    @Override
    public Long createShipOrderDetail(ShipOrderDetailSaveReqVO createReqVO) {
        // 插入
        ShipOrderDetailDO shipOrderDetail = BeanUtils.toBean(createReqVO, ShipOrderDetailDO.class);
        shipOrderDetailMapper.insert(shipOrderDetail);
        // 返回
        return shipOrderDetail.getId();
    }

    @Override
    public void updateShipOrderDetail(ShipOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateShipOrderDetailExists(updateReqVO.getId());
        // 更新
        ShipOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, ShipOrderDetailDO.class);
        shipOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteShipOrderDetail(Long shipOrderId) {
        // 删除
        shipOrderDetailMapper.delete(ShipOrderDetailDO::getShipOrderId, shipOrderId);
    }

    private void validateShipOrderDetailExists(Long id) {
        if (shipOrderDetailMapper.selectById(id) == null) {
            throw exception(SHIP_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ShipOrderDetailDO getShipOrderDetail(Long id) {
        return shipOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<ShipOrderDetailDO> getShipOrderDetailPage(ShipOrderDetailPageReqVO pageReqVO) {
        return shipOrderDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertBatch(List<ShipOrderDetailDO> shipOrderDetailDOList) {
        shipOrderDetailMapper.insertBatch(shipOrderDetailDOList);
    }

    @Override
    public List<ShipOrderDetailDO> getShipOrderDetailList(Long shipOrderId) {
        return shipOrderDetailMapper.selectList(ShipOrderDetailDO::getShipOrderId, shipOrderId);
    }
}